﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.YShow.BLL.Service
{
    public class OrderService : BaseService<OrderInfo>, IOrderService
    {

        /// <summary>
        /// 订单分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public List<OrderQuery> GetOrderList(string adddate, string enddate)
        {
            //创建List对象
            List<OrderQuery> adminUserQueries = new List<OrderQuery>();
            //创建实例
            using (var db = new YShowDBEntities())
            {
                var result = from a in db.OrderInfo
                             join b in db.Order_Detail
                             on a.Order_Id equals b.Order_Id
                             select new OrderQuery()
                             {
                                 Order_Id = a.Order_Id,
                                 Order_No = a.Order_No,
                                 Product_Name = b.Product_Name,
                                 Order_Money = a.Order_Money,
                                 District_Money = a.District_Money,
                                 CreateTime = a.CreateTime,
                                 Product_Cnt = b.Product_Cnt,
                                 Order_Status = a.Order_Status
                             };
                if (!string.IsNullOrEmpty(adddate) && !string.IsNullOrEmpty(enddate))
                {
                    DateTime time1 = Convert.ToDateTime(adddate);
                    DateTime time2 = Convert.ToDateTime(enddate);
                    return result.Where(a => a.CreateTime > time1 && a.CreateTime < time2).ToList();
                }
                else if (!string.IsNullOrEmpty(adddate))
                {
                    DateTime time1 = Convert.ToDateTime(adddate);
                    return result.Where(a => a.CreateTime > time1).ToList();
                }
                else if (!string.IsNullOrEmpty(enddate))
                {
                    DateTime time2 = Convert.ToDateTime(enddate);
                    return result.Where(a => a.CreateTime < time2).ToList();
                }
                else
                {
                    return result.ToList();
                }
            }
        }



        /// <summary>
        /// 删除两表表OrderInfo,Order_Detail
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool Delete(OrderInfo orderInfo, Order_Detail order_Detail)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除第一张表
                        db.Entry(orderInfo).State = EntityState.Deleted;

                        //删除第二张表                       
                        db.Entry(order_Detail).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }



        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool BatchDeleteById(List<OrderInfo> orderInfo, List<Order_Detail> order_Detail)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除第一张表
                        for (int i = 0; i < orderInfo.Count; i++)
                        {
                            var entity = db.OrderInfo.Find(orderInfo[i].Order_Id);
                            if (entity != null)
                            {
                                db.OrderInfo.Remove(entity);
                            }
                        }

                        //删除第二张表                       
                        for (int i = 0; i < order_Detail.Count; i++)
                        {
                            var entity = db.Order_Detail.Find(order_Detail[i].Order_Detail_Id);
                            if (entity != null)
                            {
                                db.Order_Detail.Remove(entity);
                            }
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }




        /// <summary>
        /// 修改发货状态
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool UpdateStatus(OrderInfo orderInfo, Order_Detail order_Detail)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //第一种事务
                        //修改第一张表
                        db.Entry(orderInfo).State = EntityState.Modified;


                        //修改第二张表
                        db.Entry(order_Detail).State = EntityState.Modified;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }



        /// <summary>
        /// 编辑订单信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool EditOrderInfo(OrderInfo orderInfo, Order_Detail order_Detail)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //第一种事务
                        //修改第一张表
                        db.Entry(orderInfo).State = EntityState.Modified;


                        //修改第二张表
                        db.Entry(order_Detail).State = EntityState.Modified;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }




        /// <summary>
        /// 订单类型
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public List<OrderType> OrderType()
        {
            //创建List对象
            List<OrderType> orderTypes = new List<OrderType>();
            //创建实例
            using (var db = new YShowDBEntities())
            {
                //Linq表达式
                var result = from a in db.Category
                            join b in db.Product_Spu
                            on a.Id equals b.One_Category_Id.Value into aa
                            from ab in aa.DefaultIfEmpty()
                            join c in db.Order_Detail
                            on ab.Id equals c.Product_Id into cc
                            from bc in cc.DefaultIfEmpty()
                            where a.ParentId == 0
                            group new { a, ab, bc } by new { a.Id, a.Name, ab.One_Category_Id} into e
                            select new OrderType()
                            {
                                Id = e.Key.Id,
                                CategoryName = e.Key.Name,
                                Count = e.FirstOrDefault().ab == null ? 0 : db.Order_Detail.Count(r => r.Order_Detail_Id == e.Key.One_Category_Id)
                            };
                orderTypes = result.ToList();
                return result.ToList();
            }      
        }



        /// <summary>
        /// 菜单
        /// </summary>
        /// <returns></returns>
        public List<OrderType> MenuInfo()
        {
            //创建List对象
            List<OrderType> orderTypes = new List<OrderType>();
            //创建实例
            using (var db = new YShowDBEntities())
            {
                //Linq表达式
                var result = from a in db.Category
                            join b in db.Product_Spu
                            on a.Id equals b.Two_Category_Id.Value into aa
                            from ab in aa.DefaultIfEmpty()
                            join c in db.Order_Detail
                            on ab.Id equals c.Product_Id into cc
                            from bc in cc.DefaultIfEmpty()
                            where a.ParentId != 0
                            group new { a, ab, bc } by new { a.Id, a.Name, a.ParentId, ab.One_Category_Id} into e
                            select new OrderType()
                            {
                                Id = e.Key.Id,
                                CategoryName = e.Key.Name,
                                ParentId = e.Key.ParentId,
                                Count = e.FirstOrDefault().ab == null ? 0 : db.Order_Detail.Count(r => r.Order_Detail_Id == e.Key.One_Category_Id)
                            };
                orderTypes = result.ToList();
                return result.ToList();
            }
        }
    }
}
